package edu.zzu.servideImpl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.zzu.Entity.User;
import edu.zzu.daoImpl.UserDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserService implements edu.zzu.service.UserService {
    @Autowired
    private UserDaoImpl userDao;

    @Autowired
    private ObjectMapper jsonMapper;

    @Override
    public boolean ifPermittedLogin(String tel,String password) {

        return password.equals(userDao.getPasswordByTel(tel));
    }

    @Override
    public boolean checkIfUserExistById(Long id) {
        return userDao.checkIfUserExistById(id);
    }

    @Override
    public boolean checkIfUserExistByTel(String tel) {
        return userDao.checkIfUserExistByTel(tel);
    }

    @Override
    public void register(Long id, String password, String name, int role, int status, String tel, Long transNodeId) {
        while(true){
            if(userDao.saveUser(new User(id,password,name,role,status,tel,transNodeId))>0)
                break;
        }
    }

    @Override
    public boolean modifyPassword(Long id, String newPassword) {
        return userDao.modifyPassword(id, newPassword) == 1;
    }

    @Override
    public String getUserByTel(String tel) {
        try {
            return jsonMapper.writeValueAsString(userDao.selectUserByTel(tel));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Boolean isSourceNodeHasUser(Long sourceNode) {
        return userDao.isSourceNodeHasUser(sourceNode);
    }


}
